<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 2017/6/27
 * Time: 下午8:22
 */
class Test extends MY_Cron_Controller
{
    public function test_live_end()
    {
        $ds_id  = (int)$_SERVER['argv'][2];
        $minute = (int)$_SERVER['argv'][3];


        if (!$ds_id)
        {
            exit('未输入直播ID或结束分钟数');
        }


        $this->load->library('im_api/TimRestApi');

        $api = createRestAPI();

        $ret = $api->generate_user_sig();
        if ($ret == null)
        {
            // 签名生成失败
            echo '签名生成失败';
            die();
        }


        $this->load->model('direct_seeding/Model_direct_seeding', 'ds');
        $ds_item = $this->ds->getById($ds_id);



        if ($minute)
        {
            $remind_second = ( ($minute - 1) * 60 ) + rand(35, 60);//(strtotime($v['real_end_time']) - TIMESTAMP);
            $remind_minute = ceil($remind_second / 60);
            $remind_minute = $remind_minute > 5 ? 5 : $remind_minute;


            $message = array
            (
                'remind_second' => $remind_second,
                'remind_minute' => $remind_minute,
            );


            $ret = $api->group_send_group_msg("admin", $ds_item['chat_room_num'], "@MSMK@LIVING_WILL_END@".json_encode($message));
            echo "IM消息发送状态(@MSMK@LIVING_WILL_END@):\n";print_r($ret);echo "此次推送的IM消息:\n";print_r($message);


            if (1 == $remind_minute)
            {
                //30秒以后再再同步一次
                sleep(30);


                $message['remind_second'] = $remind_second - 30;
                $ret = $api->group_send_group_msg("admin", $ds_item['chat_room_num'], "@MSMK@LIVING_WILL_END@".json_encode($message));
                echo "30秒后同步IM消息发送状态(@MSMK@LIVING_WILL_END@):\n";print_r($ret);echo "30秒后同步此次推送的IM消息:\n";print_r($message);
            }


        }
        else
        {
            $ret = $api->group_send_group_msg("admin", $ds_item['chat_room_num'], "@MSMK@LIVING_ALREADY_END@");
            echo "通知主播结束IM消息发送状态(@MSMK@LIVING_ALREADY_END@)：\n";print_r($ret);
        }
        //p($ds_item);


    }



    public function generation_user()
    {
        $this->load->model('user/Model_user', 'u');


        $start_time = strtotime('2017-03-08 00:00:00');



        for ($i = 0; $i < 340000; $i++)
        {

            $i++;
            $rand_time  = rand(1, 9663888);
            $date_time  = date('Y-m-d H:i:s', ($start_time + $rand_time));


            $nickname = $this->getRndWords(rand(1, 8));
            $recommend_code = $this->u->generation_recommend_code();


            $u_sql = 'INSERT INTO sk_user (type, lid, uname, nickname, password, mobile, recommend_type, recommend_uid, recommend_code, encryoted, status, source, openid, header, score, sex, ip, create_time)
                VALUES(0, 1, null, "'.$nickname.'", "", "", 0, 0, "'.$recommend_code.'", "'.rand_str(8).'", 1, 2, "'.rand_str(32).'", '.rand(1, 4766).', 5.0, 0, "", "'.$date_time.'")';


            $this->u->query($u_sql, false);//echo $i."\n";continue;
        }
    }


    /**
     * 随机生成中文
     *
     * @param int $num
     * @return string
     */
    private function getRndWords( $num = 8 )
    {
        $wd  = '';
        $str = require APPPATH.'config/words.php';
        for( $j=0; $j<$num;$j++ )
            $wd .= mb_substr($str,rand(1, 2000) ,1);


        return $wd;
    }

}